<template>
  <div>
    <el-dialog

      title="地址详情"
      :visible.sync="dialogVisible"
      width="60%"
      @open="dialogOpen"
      @close="handleClose"
    >
      <el-form ref="form" :model="addressForm" :rules="rules" label-width="120px" size="mini">
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="发货地址" prop="addressName">
              <el-input v-model="addressForm.addressName" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="发货人姓名">
              <el-input v-model="addressForm.name" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="发货人手机号">
              <el-input v-model="addressForm.phone" />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="邮政编码">
              <el-input v-model="addressForm.postCode" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="10" :offset="0">
            <el-form-item label="收货地址">
              <!-- <el-input v-model="addressForm.addressName" /> -->
              <el-cascader
                v-model="addressForm.selectedOptions"
                style="width:100%"
                size="mini"
                :options="options"
                @change="handleCityChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="10" :offset="0">
            <el-form-item label="详细地址">
              <el-input v-model="addressForm.detailAddress" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="doAddAddress">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
// https://www.npmjs.com/package/element-china-area-data
import { regionDataPlus, CodeToText } from 'element-china-area-data'
import {

  saveAddress as saveAddressApi,
  updateAddress as updateAddressApi
} from '@/api/order/address'
export default {
  name: 'DialogDetail',
  mixins: [],
  // props:['address']
  props: {
    address: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      options: regionDataPlus,
      dialogVisible: false,
      addressForm: {
        addressName: '郑州发货点',
        selectedOptions: ['410000', '410100', '410102']
      },
      rules: {
        addressName: [{ required: true, message: '发货地址必填', trigger: 'blur' }]
      }
    }
  },
  computed: {
  },
  watch: {
  },
  created() {
    console.log('created.....')
  },
  mounted() {
  },
  methods: {
    dialogOpen() {
      this.address.selectedOptions = this.address.cityCode.split(',')
      this.addressForm = this.address
    },
    // 对话框关闭之前的回调
    // handleBeforeClose(done) {
    //   done()
    // },
    handleCancle() {
      this.dialogVisible = false
    },
    // 关闭对话框
    handleClose() {
      // dialog内部有监听 dialogVisible 为false 就会触发close事件
      // this.$refs.form.resetFields()
      this.address = {}
      this.addressForm = this.$options.data().addressForm
    },
    handleCityChange(e) {
      console.log(e)
      this.addressForm.selectedOptions = e
    },
    showAddAddressDialog() {
      this.dialogVisible = true
    },
    doAddAddress() {
      this.$refs.form.validate(async(validate) => {
        if (validate) {
          var api = null
          if (this.addressForm.id) {
            api = updateAddressApi
          } else {
            api = saveAddressApi
          }
          // 发Ajax 请求
          this.addressForm.cityCode = this.addressForm.selectedOptions.join(',')
          this.addressForm.province = CodeToText[this.addressForm.selectedOptions[0]]
          this.addressForm.city = CodeToText[this.addressForm.selectedOptions[1]]
          this.addressForm.region = CodeToText[this.addressForm.selectedOptions[2]]
          console.log(this.addressForm)
          const res = await api(this.addressForm)
          const { success, message } = res
          if (success) {
            // 1. 关闭对话框
            this.dialogVisible = false
            // 2. 刷新数据
            this.$emit('refreshData')
          } else {
            this.$message.error(message)
          }
        } else {
          this.$message.error('注意表单校验')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
